<?php
namespace app\api\controller\service;

use app\common\controller\Api;
use think\Db;
use think\Exception;
use think\Loader;

/**
 * 首页接口
 */
class Withdraw extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function apply()
    {
        $uid = $this->auth->id;
        $userInfo = \app\api\model\service\UserInfo::getUserMoney($uid);
        $post = input('post.','','trim,strip_tags,htmlspecialchars,xss_clean');
        if(isset($post['types']) && $post['types'] == 'withdraw')
        {
            $post['user_id'] = $uid;
            $post['num'] = sprintf("%.2f",$post['num']);
            $validate = Loader::validate('service.Withdraw');
            if(!$validate->scene('add')->check($post)){
                $this->error($validate->getError());
            }
            Db::startTrans();
            try{
                \app\api\model\service\Withdraw::apply($post);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error('提现申请失败',$e->getMessage());
            }
            $this->success('提现申请已提交');
        }
        $this->success('信息返回成功',$userInfo);
    }

    public function getList()
    {
        $uid = $this->auth->id;
        $get = input('get.','');
        $get['user_id'] = $uid;
        $list = \app\api\model\service\Withdraw::getList($get);
        $this->success('列表信息返回成功',$list);
    }


    public function moneyInfo()
    {
        $uid = $this->auth->id;
        $data['money'] = \app\api\model\service\UserInfo::where(['user_id'=>$uid])->field('id,money,shop_money,shop_user_money')->find();
        $type = input('type/d',0);
        $page = input('page/d',1);
        if($type == 0)
        {
            $list = \app\api\model\service\UserMoneyLog::where(['user_id'=>$uid])->order('id desc')->page($page)->limit(10)->select();
        }elseif($type == 1){
            $list = \app\api\model\service\ShopMoneyLog::where(['user_id'=>$uid])->order('id desc')->page($page)->limit(10)->select();
        }elseif($type == 2){
            $list = \app\api\model\service\ShopUserMoneyLog::where(['user_id'=>$uid])->order('id desc')->page($page)->limit(10)->select();
        }
        $data['list'] = $list;
        $this->success('信息返回成功',$data);
    }

    public function getWithdrawDay()
    {
        $uid = $this->auth->id;
        $type = input('type/d','');
        if($type == 0){
            $config = \app\api\model\service\ProjectConfig::where(['state'=>1])->field('withdrawtype,day')->find();
        }else{
            $shopId = \app\api\model\service\Skill::where(['user_id'=>$uid])->value('shop_id');
            $config = \app\api\model\service\Shop::where(['id'=>$shopId])->field('withdrawtype,day')->find();
        }
        $this->success('信息返回成功',$config);
    }

    public function shopWithdrawList()
    {
        $uid = $this->auth->id;
        $shopId = \app\api\model\service\Shop::where(['user_id'=>$uid])->value('id');
        $get = input('get.','','trim,strip_tags');
        $get['shop_id'] = $shopId;
        $get['type'] = 1;
        $list = \app\api\model\service\Withdraw::getList($get);
        foreach ($list as $key=>$value)
        {
            $list[$key]['skill'] = \app\api\model\service\Skill::where(['user_id'=>$value['user_id']])->field('id,name,image')->find();
            $list[$key]['shop_money'] = \app\api\model\service\UserInfo::where('user_id',$value['user_id'])->value('shop_money');
        }
        $this->success('信息返回成功',$list);
    }


    public function shopCheckWithdraw()
    {
        $uid = $this->auth->id;
        $shopId = \app\api\model\service\Shop::where(['user_id'=>$uid])->value('id');
        $post = input('post.','','trim,strip_tags');
        $info = \app\api\model\service\Withdraw::where(['id'=>$post['id'],'shop_id'=>$shopId])->find();
        !$info && $this->error('当前订单无法审核');
        Db::startTrans();
        try{
            $withdraw = new \app\api\model\service\Withdraw();
            $withdraw->allowField('note,state,images')->save($post,['id'=>$post['id']]);
            if($post['state'] == -1)
            {
                \app\api\model\service\UserInfo::shopMoney(+$info['num'],$info['user_id'],'审核拒绝余额退回');
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error('提现申请失败',$e->getMessage());
        }
        $this->success('提现订单已处理');
    }


}
